//
//  ByUIPropertySafetyVC.m
//  chequanbao_ios
//
//  Created by pro on 17/5/9.
//  Copyright © 2017年 com.itboye. All rights reserved.
//

#import "ByUIPropertySafetyVC.h"

// vc
#import "ByUIBuyCarSafetyVC.h"      // 购买车险
#import "ByUISafetyOrderRecordVC.h" // 购买记录
#import "ByUICompensateVC.h"        // 理赔

// api
#import "ByApiMine.h"

@interface ByUIPropertySafetyVC ()

@property (nonatomic, strong) UIScrollView *scroll;

@property (nonatomic, strong) UILabel *myPointsLabel;

@end

@implementation ByUIPropertySafetyVC

- (void)viewDidLoad {
    [super viewDidLoad];

    self.title = @"有车有保";
    self.view.backgroundColor = [UIColor blueColor];
    
    [self addLeftBackBarButtonItem];
    
    
    
    [self createViews];
//    [self newCreateViews];
    
    [self fetchData];
}


#pragma mark - 懒加载
- (UIScrollView *)scroll {
    
    if (!_scroll) {
        _scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 64)];
        _scroll.showsVerticalScrollIndicator = NO;
        _scroll.bounces = NO;
        [self.view addSubview:_scroll];
    }
    return _scroll;
}

#pragma mark - 请求我的车险金
- (void)fetchData{
    ByNetParamsModel *model = [[ByNetParamsModel alloc] init];
    model.uid = BY_USER.uid;
    model.code_type = BY_USER.account_id;
    [ByApiMine getMinePoints:model success:^(ByNetBaseResult *result) {
        
        _myPointsLabel.text = [NSString stringWithFormat:@"我的积分：%.2f元", [[result.data objectForKey:@"score1"] floatValue]/100.0];
//        _myPointsLabel.text = [NSString stringWithFormat:@"我的车险金：100积分"];

    } failure:^(ByNetBaseResult *result) {
        [ByCoreToolAlert showFailureWithText:@"收入请求失败"];
    }];
}

#pragma mark - 创建视图
- (void)createViews {
    
    CGFloat x, y, width, height;
    
    x = 10;
    y = 0;
    width = SCREEN_WIDTH - x * 2;
    height = 50;
    
    /* 
     我的车现金
     */
    UIView *bgView1 = [[UIView alloc] initWithFrame:CGRectMake(x, y, width, height)];
    [self.scroll addSubview:bgView1];
    bgView1.layer.cornerRadius = height / 2;
    bgView1.backgroundColor = [UIColor colorWithWhite:1 alpha:0.2];
    
    x = 0;
    y = 0;
    width = 20;
    height = 20;
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(x, y, width, height)];
    imageView.image = [UIImage imageNamed:@"mine_points"];
    [bgView1 addSubview:imageView];
    
    x = 0;
    y = 0;
    NSString *text = @"我的积分：0积分";
    width = [text getSizeOfString:16].width + 2;
    height = 20;
    _myPointsLabel = [[UILabel alloc] initWithFrame:CGRectMake(x, y, width, height)];
    _myPointsLabel.adjustsFontSizeToFitWidth = YES;
    _myPointsLabel.font = FONT(16);
    _myPointsLabel.text = text;
    _myPointsLabel.textColor = [UIColor whiteColor];
    _myPointsLabel.textAlignment = NSTextAlignmentLeft;
    [bgView1 addSubview:_myPointsLabel];
    
    
    CGFloat remain = (bgView1.width - imageView.width - _myPointsLabel.width - 5) / 2;
    imageView.left = remain;
    imageView.y = bgView1.height / 2;
    _myPointsLabel.left = imageView.right + 5;
    _myPointsLabel.y = bgView1.height / 2;
    
    
    /* 
     购买车险和购买记录
     */
    x = bgView1.left;
    y = bgView1.bottom + 15;
    width = bgView1.width;
    height = 200;
    UIView *bgView2 = [[UIView alloc] initWithFrame:CGRectMake(x, y, width, height)];
    [self.scroll addSubview:bgView2];
    bgView2.layer.cornerRadius = 4;
    bgView2.backgroundColor = [UIColor colorWithWhite:1 alpha:0.2];
    
    NSArray *titles = @[@"车驾咨询", @"历史记录", @"服务热线"];
    NSArray *images = @[@"Safety_caichan_goumaichexian", @"Safety_caichan_goumaijilu", @"Safety_caichan_lipei"];
    for (int i = 0; i < titles.count; i ++) {
        width = bgView2.width / 3;
        height = bgView2.height;
        x = i * width;
        y = 0;
        UIControl *baseView = [[UIControl alloc] initWithFrame:CGRectMake(x, y, width, height)];
        [bgView2 addSubview:baseView];
        baseView.tag = i + 1;
        [baseView addTarget:self action:@selector(tapAction:) forControlEvents:UIControlEventTouchUpInside];
        
        x = 0;
        y = 0;
//        width = baseView.width * 0.85;
        height = width = 50;
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(x, y, width, height)];
        imageView.image = [UIImage imageNamed:images[i]];
        [baseView addSubview:imageView];
        
        x = 0;
        y = 0;
//        width = imageView.width;
        width = baseView.width * 0.85;
        height = 30;
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(x, y, width, height)];
        label.adjustsFontSizeToFitWidth = YES;
        label.font = B_FONT(18);
        label.text = titles[i];
        label.textColor = [UIColor whiteColor];
        label.textAlignment = NSTextAlignmentCenter;
        [baseView addSubview:label];
        
        UIView *lineView;
        if (i != 2) {
            x = 0;
            y = 20;
            width = 0.5;
            height = baseView.height - 40;
            lineView = [[UIView alloc] initWithFrame:CGRectMake(x, y, width, height)];
            lineView.backgroundColor = [UIColor colorWithWhite:0.95 alpha:1];
            [baseView addSubview:lineView];
        }
   
        
        CGFloat margin = 20;
        remain = (baseView.height - imageView.height - label.height - margin) / 2;
        imageView.top = remain;
        imageView.x = baseView.width / 2;
        label.x = imageView.x;
        label.top = imageView.bottom + margin;
        lineView.x = baseView.width - 0.5;
    }
    
    
    
    /*
     合作机构
     */
    x = 10;
    y = bgView2.bottom + 15;
    width = bgView2.width;
    height = 20;
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(x, y, width, height)];
    titleLabel.font = FONT(16);
    titleLabel.text = @"战略合作：";
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.textAlignment = NSTextAlignmentLeft;
    [self.scroll addSubview:titleLabel];
    
    x = 0;
    y = titleLabel.bottom + 5;
    width = SCREEN_WIDTH;
    height = width;
    UIView *bgView3 = [[UIView alloc] initWithFrame:CGRectMake(x, y, width, height)];
    [self.scroll addSubview:bgView3];
    bgView3.backgroundColor = [UIColor colorWithWhite:1 alpha:1];
    
    images = @[
//               @"Safety_caichan_hezuo_zhongguorenshou",
//               @"Safety_caichan_hezuo_yangguangbaoxian",
//               @"Safety_caichan_hezuo_zhonghuabaoxian",
               @"Safety_caichan_zhongguprenmin",
               @"Safety_caichan_hezuo_tiananbaoxian",
               @"Safety_caichan_hezuo_AB",
               @"Safety_caichan_zhonghualianhe",
//               @"Safety_caichan_hezuo_taipingyangbaoxian",
//               @"Safety_caichan_hezuo_NCI",
//               @"Safety_caichan_hezuo_zhongguodadibaoxian",
//               @"Safety_caichan_hezuo_huashoubaoxian",
               ];
    for (int i = 0; i < images.count; i ++) {
        width = bgView3.width / 3;
        height = width;
        x = (i % 3) * width;
        y = (i / 3) * height;
        UIControl *baseView = [[UIControl alloc] initWithFrame:CGRectMake(x, y, width, height)];
        [bgView3 addSubview:baseView];
        baseView.tag = i + 1;
        
        x = 0;
        y = 0;
        width = baseView.width;
        height = width;
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(x, y, width, height)];
        imageView.image = [UIImage imageNamed:images[i]];
        [baseView addSubview:imageView];
    }
    
    UIControl *baseView4 = [bgView3 viewWithTag:4];
    x = baseView4.right + 20;
    y = 0;
    width = SCREEN_WIDTH - x;
    height = 30;
    UILabel *infoLabel = [[UILabel alloc] initWithFrame:CGRectMake(x, y, width, height)];
    infoLabel.bottom = baseView4.center.y;
    infoLabel.text = @"更多合作伙伴正在持续加入";
    infoLabel.textColor = [UIColor lightGrayColor];
    infoLabel.layer.cornerRadius = 30 / 2;
    infoLabel.font = FONT(14);
    infoLabel.layer.borderColor = [UIColor colorWithWhite:0.95 alpha:1].CGColor;
    infoLabel.textAlignment = NSTextAlignmentCenter;
    infoLabel.layer.borderWidth = 1;
    infoLabel.adjustsFontSizeToFitWidth = YES;
    [bgView3 addSubview:infoLabel];
    
    
    
    
    NSInteger col = 3;
    NSInteger row = 3;
    CAShapeLayer *lineLayer = [CAShapeLayer layer];
    lineLayer.lineWidth = 0.5;
    lineLayer.strokeColor = [UIColor colorWithWhite:1 alpha:0.6].CGColor;
    UIBezierPath *path = [UIBezierPath bezierPath];
    for (int i = 1; i < col; i ++) {
        [path moveToPoint:CGPointMake(bgView3.width / col * i, 0)];
        [path addLineToPoint:CGPointMake(bgView3.width / col * i, bgView3.height)];
    }
    for (int i = 1; i < row; i ++) {
        [path moveToPoint:CGPointMake(0, bgView3.height / row * i)];
        [path addLineToPoint:CGPointMake(bgView3.width, bgView3.height / row * i)];
    }
    lineLayer.path = path.CGPath;
    [bgView3.layer addSublayer:lineLayer];
    
    
    
    /*
     设置scroll
     */
    self.scroll.contentSize = CGSizeMake(0, bgView3.bottom);
}


#warning 废弃
- (void)newCreateViews {
    
    CGFloat x, y, width, height;
    
    /*
     合作机构
     */
    x = 0;
    y = 0;
    width = SCREEN_WIDTH;
    height = width;
    UIView *bgView3 = [[UIView alloc] initWithFrame:CGRectMake(x, y, width, height)];
    [self.view addSubview:bgView3];
    bgView3.backgroundColor = [UIColor colorWithWhite:1 alpha:0.2];
    bgView3.bottom = SCREEN_HEIGHT;
    
    NSInteger col = 3;
    NSInteger row = 3;
    CAShapeLayer *lineLayer = [CAShapeLayer layer];
    lineLayer.lineWidth = 0.5;
    lineLayer.strokeColor = [UIColor colorWithWhite:1 alpha:0.6].CGColor;
    UIBezierPath *path = [UIBezierPath bezierPath];
    for (int i = 1; i < col; i ++) {
        [path moveToPoint:CGPointMake(bgView3.width / col * i, 0)];
        [path addLineToPoint:CGPointMake(bgView3.width / col * i, bgView3.height)];
    }
    for (int i = 1; i < row; i ++) {
        [path moveToPoint:CGPointMake(0, bgView3.height / row * i)];
        [path addLineToPoint:CGPointMake(bgView3.width, bgView3.height / row * i)];
    }
    lineLayer.path = path.CGPath;
    [bgView3.layer addSublayer:lineLayer];
    
    
    /*
     合作机构
     */
    x = 10;
    y = 0;
    width = SCREEN_WIDTH - x * 2;
    height = 20;
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(x, y, width, height)];
    titleLabel.font = FONT(16);
    titleLabel.text = @"战略合作：";
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.textAlignment = NSTextAlignmentLeft;
    [self.view addSubview:titleLabel];
    titleLabel.bottom = bgView3.top;
    
    
    
    
    /*
     我的车现金
     */
    x = 10;
    y = 64;
    width = SCREEN_WIDTH - x * 2;
    height = 50;
    UIView *bgView1 = [[UIView alloc] initWithFrame:CGRectMake(x, y, width, height)];
    [self.view addSubview:bgView1];
    bgView1.layer.cornerRadius = height / 2;
    bgView1.backgroundColor = [UIColor colorWithWhite:1 alpha:0.2];
    
    x = 0;
    y = 0;
    width = 20;
    height = 20;
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(x, y, width, height)];
    imageView.image = [UIImage imageNamed:@"mine_points"];
    [bgView1 addSubview:imageView];
    
    x = 0;
    y = 0;
    NSString *text = @"我的车现金：0积分";
    width = [text getSizeOfString:16].width + 2;
    height = 20;
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(x, y, width, height)];
    label.adjustsFontSizeToFitWidth = YES;
    label.font = FONT(16);
    label.text = text;
    label.textColor = [UIColor whiteColor];
    label.textAlignment = NSTextAlignmentLeft;
    [bgView1 addSubview:label];
    
    
    CGFloat remain = (bgView1.width - imageView.width - label.width - 5) / 2;
    imageView.left = remain;
    imageView.y = bgView1.height / 2;
    label.left = imageView.right + 5;
    label.y = bgView1.height / 2;
    
    /*
     购买车险，购买记录，理赔
     */
    x = 10;
    y = bgView1.bottom + 10;
    width = bgView1.width;
    height = titleLabel.top - 10 - y;
    UIView *bgView2 = [[UIView alloc] initWithFrame:CGRectMake(x, y, width, height)];
    [self.view addSubview:bgView2];
    bgView2.layer.cornerRadius = 4;
    bgView2.backgroundColor = [UIColor colorWithWhite:1 alpha:0.2];
    
    NSArray *titles = @[@"车险咨询", @"购买记录", @"理赔"];
    for (int i = 0; i < titles.count; i ++) {
        width = bgView2.width / 3;
        height = bgView2.height;
        x = i * width;
        y = 0;
        UIControl *baseView = [[UIControl alloc] initWithFrame:CGRectMake(x, y, width, height)];
        [bgView2 addSubview:baseView];
        
        x = 0;
        y = 0;
        width = baseView.width * 0.8;
        height = width;
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(x, y, width, height)];
        imageView.image = DEFAULT_IMAGE;
        [baseView addSubview:imageView];
        
        x = 0;
        y = 0;
        width = imageView.width;
        height = 30;
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(x, y, width, height)];
        label.adjustsFontSizeToFitWidth = YES;
        label.font = FONT(20);
        label.text = titles[i];
        label.textColor = [UIColor whiteColor];
        label.textAlignment = NSTextAlignmentCenter;
        [baseView addSubview:label];
        
        CGFloat margin = 10;
        remain = (baseView.height - imageView.height - label.height - margin) / 2;
        imageView.top = remain;
        imageView.x = baseView.width / 2;
        label.x = imageView.x;
        label.top = imageView.bottom + margin;
    }
}

- (void)tapAction:(UIControl *)tap {
    
    if (tap.tag == 1) {
        ByUIBuyCarSafetyVC *vc = [[ByUIBuyCarSafetyVC alloc] init];
        [self by_normalPush:vc hasNavi:YES];
    }
    if (tap.tag == 2) {
        ByUISafetyOrderRecordVC *vc = [[ByUISafetyOrderRecordVC alloc] init];
        [self by_normalPush:vc hasNavi:YES];
    }
    if (tap.tag == 3) {
        ByUICompensateVC *vc = [[ByUICompensateVC alloc] init];
        [self by_normalPush:vc hasNavi:YES];
    }
}

@end
